<%@ Import Namespace="Wikey.Utilities.Extensions" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Wikey.Entity.Page>" %>
<%@ Import Namespace="Wikey.Domain.Components"%>
<%@ Import Namespace="Wikey.Domain.Repository"%>

<% var list = Factory.Get<ISpaceRepository>().SelectAll(); %>
<div id="SpaceSelection">
<span id="CurrentSpaceSelection">
  <% if ((bool)ViewData["HasSpace"]) {%>
      <%=ViewData["SpaceName"] %>
  <%}else{ %>
    Click to select a space
  <%} %>
 </span>
<input type="hidden" value="<%= ViewData["SpaceId"] %>" id="SpaceId" />
<div id="PageSpaces" >
<ul id="PageSpaceList">
<% foreach (var space in list) { %>
       <li>
          <span id='space-<%=space.Id %>'>
            <%= space.Name %>
          </span>
       </li>
   <%} %>
   </ul>
</div>
</div>
<script type="text/javascript">
  $(function() {
    $("#PageSpaceList").find(" li span ").each(function(item, index) {
      $(this).click(function(ref) {
        idParts = this.id;
        id = idParts.split('-')[1];
        SelectSpace(id,$(this).text());
      });
    });
    $("#CurrentSpaceSelection").click(function() {
      $("#PageSpaces").show();
    });
  });

  function SelectSpace(id,name) {
    $("#SpaceId").val(id);
    $("#CurrentSpaceSelection").text(name);
    $("#PageSpaces").hide();
  }
</script>